const api = require('../../api/api')
const app = getApp()
const util = require('../../utils/js/util')
Page({
  data: {
    list: [], // 列表
    page: 1, // 页码
    listStatus: 'loading', // 状态

  },
  onLoad() {
    this.getlist()
  },
  gotoans({ currentTarget: { dataset: { item } } }) {
    if(item.status == 0) return util.toast('请耐心等待...')
    if(item.status == 1) return wx.navigateTo({ url: '../reportDetail/index?detail=' + encodeURIComponent(JSON.stringify(item)) })
  },
  onReachBottom() {
    if (this.data.listStatus === 'tobottom') return
    this.setData({ page: this.data.page + 1 })
    this.getlist()
  },
  // 上传报告
  async upload() {
    wx.showActionSheet({
      itemList: ['上传pdf', '上传图片'],
      success: ({ tapIndex }) => {
        if (tapIndex == 0) {
          wx.chooseMessageFile({
            count: 1,
            type: 'file',
            success: res => {
              wx.uploadFile({
                url: "https://jk.demo-dev.cn/api/report/uploadReport",
                filePath: res.tempFiles[0].path,
                name: 'file',
                header: { "token": app.globalData.userInfo.token },
                success: res1 => {
                  if (res1.statusCode === 200 && res1.errMsg === "uploadFile:ok") {
                    const res2 = JSON.parse(res1.data)
                    if (res2.code === 1) {
                      util.toast(res2.msg)
                      this.setData({ page: 1, listStatus: 'loading' })
                      this.getlist()
                    } else {
                      util.toast(res2.msg)
                    }
                  }
                }
              })
            }
          })
        } else if (tapIndex == 1) {
          wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            success: async res => {
              wx.uploadFile({
                url: "https://jk.demo-dev.cn/api/report/uploadReport",
                filePath: res.tempFiles[0].tempFilePath,
                name: 'file',
                header: { "token": app.globalData.userInfo.token },
                success: res1 => {
                  if (res1.statusCode === 200 && res1.errMsg === "uploadFile:ok") {
                    const res2 = JSON.parse(res1.data)
                    if (res2.code === 1) {
                      util.toast(res2.msg)
                      this.setData({ page: 1, listStatus: 'loading' })
                      this.getlist()
                    } else {
                      util.toast(res2.msg)
                    }
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  // 获取报告列表
  async getlist() {
    const { data, current_page, last_page } = await api.getReportList({ p: this.data.page })
    const newdata = data.map(v => {
      const files = v.image_path.split('.')
      return {
        ...v,
        createtime: util.getDateTime(v.createtime * 1000).format('M-D h:m'),
        updatetime: util.getDateTime(v.updatetime * 1000).format('M-D h:m'),
        fileType: files[files.length - 1]
      }
    })
    this.setData({
      list: this.data.page == 1 ? newdata : this.data.list.concat(newdata),
      listStatus: current_page == last_page ? 'tobottom' : current_page < last_page ? 'loading' : current_page > last_page ? 'nodata' : ''
    })
  },
  // 预览图片
  previmg({ currentTarget: { dataset: { item } } }) {
    if (item.fileType == 'pdf') {
      wx.downloadFile({
        url: item.image_path,
        success: res => {
          console.log(res.tempFilePath);
          wx.openDocument({
            filePath: res.tempFilePath,
            fileType: 'pdf',
            success: res1 => {
              console.log(res1);
            }
          })
        }
      })
    } else {
      util.prevImg([item.image_path], item.image_path)
    }
  },
})
